/*
 * Copyright 2011 CraneLeeon authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.crane.webtemplate.action;


import org.crane.webtemplate.FormatSpec;
import org.crane.webtemplate.exception.TextMessageException;
import java.util.Date;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.exception.ConstraintViolationException;
import org.json.JSONObject;
import static org.crane.webtemplate.Tokens.*;

/**
 *
 * @author CraneLeeon
 */
@Action("/DataExportFromClientSide")
@Results({
    @Result(name = "json", type = "dispatcher", location = "/WEB-INF/jsp/json.jsp"),
    @Result(name = "xml", type = "dispatcher", location = "/WEB-INF/jsp/xml.jsp"),
    @Result(name = "csv", type = "dispatcher", location = "/WEB-INF/jsp/csv.jsp")
})
public class DataExportFromClientSide extends BaseAction{

    protected static final Log logger = LogFactory.getLog(DataExportFromClientSide.class);
    private String data;
    private String fileName;
    public static String CHARSET = "UTF-8";

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public  Class getBoClass(){
        return null;
    }

    @Override
    public String execute() throws Exception {
        try {
            switch (this.act) {
                case ACT_EXPORT_CSV: exportCSV(); return "csv";
            }
        } catch (TextMessageException ex) {
            this.json = new JSONObject().put("success", false).put("info", getText(ex.getMessage())).toString();
        } catch (ConstraintViolationException ex) {
            logger.debug(ex);
            this.json = new JSONObject().put("success", false).put("info", getText("ConstraintViolationException")).toString();
        } catch (Exception ex) {
            logger.debug(ex);
            this.json = new JSONObject().put("success", false).put("info", ex).toString();
        }
        return "json";
    }

    private void exportCSV() throws Exception {
        this.servletResponse.setCharacterEncoding(CHARSET);
        if(this.fileName != null){
            this.servletResponse.addHeader("Content-Disposition", "attachment; filename="+this.fileName+FormatSpec.FORMATTER_INTEGER_DATETIME.format(new Date())+".csv");
        }else{
            this.servletResponse.addHeader("Content-Disposition", "attachment; filename=data"+FormatSpec.FORMATTER_INTEGER_DATETIME.format(new Date())+".csv");
        }
        this.csv = this.data;
    }
 

}
